package me.firstapp.repository.bbs.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import me.firstapp.common.utils.Page;
import me.firstapp.module.bbs.Collection;
import me.firstapp.repository.base.impl.EntityRepositoryImpl;
import me.firstapp.repository.bbs.CollectionRepository;

@Repository
public class CollectionRepositoryImpl extends EntityRepositoryImpl<Collection> implements CollectionRepository {

	@Override
	public Long findCollectionCounts(Long memberId, Long topicId, Long productId, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT COUNT(bean.id) FROM Collection bean WHERE 1=1 ");

		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		
		if (memberId != null) {
			sql.append(" AND bean.member.id=:memberId ");
			parameters.put("memberId", memberId);
		} else if (topicId != null) {
			sql.append(" AND bean.topic.id=:topicId ");
			parameters.put("topicId", topicId);
		} else if (productId != null) {
			sql.append(" AND bean.product.id=:productId ");
			parameters.put("productId", productId);
		}

		List<Long> countList = super.query(Long.class, sql.toString(), parameters);
		return countList.isEmpty() ? 0 : countList.get(0);
	}

	@Override
	public Collection findMemberTopicCollectionRel(Long memberId, Long topicId, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Collection bean WHERE 1=1 ");
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.member.id=:memberId ");
		parameters.put("memberId", memberId);
		sql.append(" AND bean.topic.id=:topicId ");
		parameters.put("topicId", topicId);
		List<Collection> results = super.query(Collection.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Collection findMemberProductCollectionRel(Long memberId, Long productId, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Collection bean WHERE 1=1 ");
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.member.id=:memberId ");
		parameters.put("memberId", memberId);
		sql.append(" AND bean.product.id=:productId ");
		parameters.put("productId", productId);
		List<Collection> results = super.query(Collection.class, sql.toString(), parameters);
		return results.isEmpty() ? null : results.get(0);
	}

	@Override
	public Page<Collection> findMemberCollectionsPage(Integer pageNo, Integer pageSize, Long memberId, Integer type, Long apiClientId) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder(" FROM Collection bean WHERE 1=1 ");
		
		sql.append(" AND bean.apiClientId =:apiClientId ");
		parameters.put("apiClientId", apiClientId);
		sql.append(" AND bean.member.id=:memberId ");
		parameters.put("memberId", memberId);
		sql.append(" AND bean.type=:type ");
		parameters.put("type", type);
		sql.append(" ORDER BY bean.insertedAt DESC ");
		return super.queryForPage(Collection.class, sql.toString(), parameters, pageNo, pageSize);
	}

	@Override
	protected Class<Collection> getPersistentClass() {
		return Collection.class;
	}

}
